package com.gjy.util.steam;

import com.gjy.util.entity.Pojo;

import java.util.*;
import java.util.function.BiConsumer;
import java.util.function.BinaryOperator;
import java.util.function.Function;
import java.util.function.Supplier;
import java.util.stream.Collector;

/**
 * @author 宫静雨
 * @version 1.0
 * @since 2023-11-06 11:35:07
 */
public class PojoCollectToList implements Collector<Pojo, List<Pojo>, List<Pojo>> {
    private static void accept(List<Pojo> list, Pojo pojo) {
        if (list.stream().noneMatch(old -> old.getOrderNo().equals(pojo.getOrderNo()))) {
            list.add(pojo);
        } else {
            list.forEach(o -> {
                if (o.getOrderNo().equals(pojo.getOrderNo())) {
                    o.setDisposeTime(o.getDisposeTime() + "\n" + pojo.getDisposeTime());
                }
            });
        }
    }

    @Override
    public Supplier<List<Pojo>> supplier() {
        return ArrayList::new;
    }

    @Override
    public BiConsumer<List<Pojo>, Pojo> accumulator() {
        return PojoCollectToList::accept;
    }

    @Override
    public BinaryOperator<List<Pojo>> combiner() {
        return (l, f) -> {
            l.addAll(f);
            return l;
        };
    }

    @Override
    public Function<List<Pojo>, List<Pojo>> finisher() {
        return Function.identity();
    }

    @Override
    public Set<Characteristics> characteristics() {
        return Collections.unmodifiableSet(EnumSet.of(Characteristics.IDENTITY_FINISH));
    }

}
